import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { router } from 'umi';
import { fetchValidation } from '@/utils';
import operationSubmit from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    submit: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'cloudBoxAdd',

  state: {

  },

  effects: {
    * submit({ payload }, { call, put }) {
      const response = yield call(operationSubmit, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('添加成功');
        yield put({
          type: 'global/setPath',
        });
        router.goBack();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        dataInfo: action.payload,
      };
    },
  },
};

export default Model;
